package com.lfs.ui;

import com.lfs.domain.BackendResponse;
import com.lfs.domain.CaptchaResponse;
import com.lfs.service.AccountService;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;

/**
 * 注册对话框
 */
public class RegisterDialog extends JDialog {

    private final JTextField usernameField = new JTextField(20);
    private final JPasswordField passwordField = new JPasswordField(20);
    private final JPasswordField confirmPasswordField = new JPasswordField(20);
    private final JTextField captchaField = new JTextField(10);
    private final JLabel captchaLabel = new JLabel();
    private String captchaId;

    private final AccountService accountService = new AccountService();

    public RegisterDialog(Frame owner) {
        super(owner, "用户注册", true);
        initUI();
    }

    private void initUI() {
        setLayout(new BorderLayout(15, 15));
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 用户名
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("用户名:"), gbc);
        gbc.gridx = 1;
        panel.add(usernameField, gbc);

        // 密码
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JLabel("密码:"), gbc);
        gbc.gridx = 1;
        panel.add(passwordField, gbc);

        // 确认密码
        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(new JLabel("确认密码:"), gbc);
        gbc.gridx = 1;
        panel.add(confirmPasswordField, gbc);

        // 验证码
        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(new JLabel("验证码:"), gbc);
        gbc.gridx = 1;
        JPanel captchaPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        captchaPanel.add(captchaField);
        captchaPanel.add(captchaLabel);
        JButton refreshCaptcha = new JButton("刷新");
        refreshCaptcha.addActionListener(e -> refreshCaptcha());
        captchaPanel.add(refreshCaptcha);
        panel.add(captchaPanel, gbc);

        // 按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(this::performRegistration);
        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> setVisible(false));
        buttonPanel.add(registerButton);
        buttonPanel.add(cancelButton);

        add(panel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 初始化验证码
        refreshCaptcha();

        pack();
        setLocationRelativeTo(getOwner());
    }

    private void refreshCaptcha() {
        SwingWorker<CaptchaResponse, Void> worker = new SwingWorker<>() {
            @Override
            protected CaptchaResponse doInBackground() throws Exception {
                return accountService.getCaptcha();
            }

            @Override
            protected void done() {
                try {
                    CaptchaResponse response = get();
                    captchaId = response.getCaptchaId();
                    byte[] imageBytes = response.getImageData();
                    BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
                    captchaLabel.setIcon(new ImageIcon(image));
                    pack();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(RegisterDialog.this, "获取验证码时发生错误: " + e.getMessage(), "严重错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        };
        worker.execute();
    }

    private void performRegistration(ActionEvent e) {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String captchaInput = captchaField.getText();

        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名和密码不能为空。", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致。", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (captchaInput.isEmpty()) {
            JOptionPane.showMessageDialog(this, "验证码不能为空。", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        LoadingDialog loadingDialog = new LoadingDialog(this);

        SwingWorker<BackendResponse<Object>, Void> worker = new SwingWorker<BackendResponse<Object>, Void>() {
            @Override
            protected BackendResponse<Object> doInBackground() throws Exception {
                return accountService.register(username, password, captchaInput, captchaId);
            }

            @Override
            protected void done() {
                loadingDialog.setVisible(false);
                loadingDialog.dispose();
                try {
                    BackendResponse<Object> response = get();
                    if (response.getCode() == 200) {
                        JOptionPane.showMessageDialog(RegisterDialog.this, "注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                        setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(RegisterDialog.this, "注册失败: " + response.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                        refreshCaptcha();
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(RegisterDialog.this, "注册过程中发生错误: " + ex.getMessage(), "严重错误", JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                    refreshCaptcha();
                }
            }
        };

        worker.execute();
        loadingDialog.setVisible(true);
    }
}
